package com.zeta.api.config;

import cn.dev33.satoken.same.SaSameUtil;
import cn.dev33.satoken.stp.StpUtil;
import feign.Logger;
import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.zetaframework.core.constants.Constants;
import org.zetaframework.core.utils.ContextUtil;
import org.zetaframework.core.utils.MdcContextUtil;

/**
 * feign默认配置
 * <p>
 * 用法：
 * <pre>{@code
 *    // 在每个服务的启动类上添加如下代码
 *    @EnableFeignClients(basePackages = {"com.zeta.api.client"}, defaultConfiguration = DefaultFeignConfiguration.class)
 * }</pre>
 *
 * @author gcc
 */
public class DefaultFeignConfiguration {

    /**
     * feign日志级别
     */
    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

    /**
     * feign拦截器
     */
    @Bean
    public RequestInterceptor feignInterceptor() {
        return requestTemplate -> {
            // 请求携带token
            requestTemplate.header(StpUtil.getTokenName(), ContextUtil.getToken());
            // 请求携带same-token
            requestTemplate.header(SaSameUtil.SAME_TOKEN, ContextUtil.getSameToken());
            // 请求携带traceId
            requestTemplate.header(Constants.TRACE_ID, MdcContextUtil.getTraceId());
        };
    }

}
